import { Component, OnInit } from '@angular/core';
import { PlatformuserService } from '../../service/platformuser.service';
import { NzMessageService } from 'ng-zorro-antd';
import { CommonService } from '../../service/common.service';
import { MerchantService } from '../../service/merchant.service';
import { Router } from '@angular/router';
import { CityService } from 'src/app/service/city.service';
import { RoleRight } from 'src/kalend';
@Component({
  selector: 'app-partnerlist',
  templateUrl: './partnerlist.component.html',
  styleUrls: ['./partnerlist.component.scss'],
  providers: [PlatformuserService, MerchantService, CityService]
})
export class PartnerlistComponent implements OnInit {
  name: any;
  role: any;
  pageIndex = 1;
  pageSize = 15;
  total = 1;
  dataSet = [];
  loading = true;
  isOkLoading = false;
  isVisible = false;
  roleItems: Array<any> = []
  doc: any = {};
  status: any;
  statuslist = [{ key: null, value: '全部' }, { key: 1, value: '有效' }, { key: 0, value: '禁用' }];
  areas: Array<any> = [];
  nzOptions: Array<any> = [];
  isadmin = RoleRight.getisRole(null, '系统管理员');

  // tslint:disable-next-line:max-line-length
  constructor(private userservice: PlatformuserService, private msg: NzMessageService,
    private commonservice: CommonService, private service: MerchantService, private router: Router, private cityservice: CityService) {
    this.cityservice.querys().then((res) => {
      this.nzOptions = res;
    });
  }

  ngOnInit() {
    this.searchData();
  }

  searchData(reset: boolean = false): void {
    this.loading = true;
    this.userservice.query({
      offset: this.pageIndex,
      limit: this.pageSize,
      name: this.name,
      status: this.status,
      role: '5b94cd656bbe2937c8fe5882',
      areas: this.areas
    }).then((res) => {
      this.loading = false;
      this.total = res.total;
      this.dataSet = res.rows;
    });
  }

  // tslint:disable-next-line:member-ordering
  datain: any = {
    close: () => {
      this.isVisible = false;
      this.datain.loading = false;
      this.datain.total = 0;
      this.datain.dataSet = [];
      this.datain.id = null;
    }
  }

  /**新增 */
  add() {
    this.isVisible = true;
  }

  /**修改 */
  edit(id: any) {
    this.isVisible = true;
    this.userservice.queryById(id).then((res) => {
      this.doc = res;
    });
  }
  /**禁用 */
  lock(id: any) {
    this.commonservice.confirm('确定要禁用用户吗?', () => {
      this.userservice.lock(id).then((res) => {
        this.msg.success('用户已禁用！');
        this.searchData();
      });
    })
  }
  /**启用 */
  open(id: any) {
    this.commonservice.confirm('确定要启用用户吗?', () => {
      this.userservice.open(id).then((res) => {
        this.msg.success('用户已启用！');
        this.searchData();
      });
    })
  }
  /** 邀请记录 */
  inverlist(id: any) {
    this.router.navigate([`/manage/userwinhistory/${id}`]);
  }
  /** 关联餐厅 */
  look(id: any) {
    this.router.navigate(['/manage/merchant', id]);
  }
  handleOk(): void {
    this.isOkLoading = true;
    if (!this.doc.bank || this.doc.bank === '') {
      this.msg.warning('请输入开户行');
    } else if (!this.doc.bankno || this.doc.bankno === '') {
      this.msg.warning('请输入银行卡号');
    } else {
      this.userservice.update(this.doc).then((res) => {
        this.msg.success('修改成功！');
        this.searchData();
        this.isVisible = false;
        this.doc = {};
      });
    }

  }


  handleCancel(): void {
    this.isVisible = false;
    this.doc = {};
  }
  onChanges(values: any): void {
    console.log(values, this.areas);
  }
}
